Forms are an essential part of web development, allowing users to input and submit data. HTML provides various
form elements, each with its own attributes that define behavior, validation, and styling.
In this guide, we will explore all the important attributes used in HTML form elements, including input, textarea,
select, button, and more.
Common Attributes for Form Elements
Most form elements share some common attributes that control their behavior.
The name Attribute
The name attribute specifies a unique identifier for the input field when submitting form data.
The name="username" attribute ensures the input value is sent as username=value when the form is submitted.
The id and for Attributes
The id attribute gives a unique identifier to the input field.
The for attribute in <label> links the label to the input, improving accessibility.
The value Attribute
The value attribute sets the default value for an input field.
The placeholder Attribute
The placeholder attribute displays temporary text inside an input field before the user types anything.
The required Attribute
The required attribute makes an input field mandatory. The form cannot be submitted if the field is empty.
The readonly Attribute
The readonly attribute prevents users from editing the input field but still allows them to copy the value.
The disabled Attribute
The disabled attribute disables an input field, making it unclickable and preventing it from being submitted.
The maxlength and minlength Attributes
maxlength limits the maximum number of characters.
minlength ensures a minimum number of characters is entered.
The autofocus Attribute
The autofocus attribute makes an input field automatically focused when the page loads.
The multiple Attribute
The multiple attribute allows users to select multiple values in <input> and <select>.
Attributes for <textarea>
The rows and cols Attributes
Control the height and width of the <textarea>.
The wrap Attribute
Defines how text wraps inside the <textarea>.
wrap="soft" → Text does not wrap; scrolls horizontally.